package com.ytktt.study.week9.task3.rpcfx.server;

import com.ytktt.study.week9.task3.rpcfx.enums.ErrResponseEnum;
import com.ytktt.study.week9.task3.rpcfx.exception.RpcfxException;
import com.ytktt.study.week9.task3.rpcfx.server.properties.RpcfxServerProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Rpcfx server
 *
 * @author ytk
 */
@Slf4j
@RequiredArgsConstructor
public abstract class RpcfxServer {

    private final RpcfxServerProperties rpcfxServerProperties;
    private final RpcfxInvoker rpcfxInvoker;

    public void run() {
        log.info("{} {} starting...", this.rpcfxServerProperties.getName(), this.rpcfxServerProperties.getVersion());
        try {
            this.start(this.rpcfxServerProperties, this.rpcfxInvoker);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RpcfxException(ErrResponseEnum.SERVER_ERROR);
        } finally {
            this.clear();
        }
    }

    public abstract void start(RpcfxServerProperties rpcfxServerProperties, RpcfxInvoker rpcfxInvoker) throws Exception;

    public abstract void clear();
}
